
$(function(){
	$('.nav-mobile img').click(function(){
		$('.navbar-nav').toggle();
		$('.navbar-nav').css('background-color','#000000');
	});
	//载入消息.现在每个界面都是需要的.
	loadMsg();
});

/**
 * 注销登陆.
 */
function logout(){
	if(typeof deuids !="undefined"){
		delCookie(deuids);
	}
	var rootPath = "login";
	var op = {};
	op.url = "root.json";
	jQuery.ajax({
		url:op.url
		,data:{action:"logout",os:"PC"}
		,type:"post"
		,dataType:"json"
		,timeout:10000
		,success:function(data){
		}
		,error:function(XMLHttpRequest, textStatus, errorThrown){
		}
		,complete:function(){
			window.parent.location.href = getBasePath()+rootPath;
		}
	});
}
function delCookie(key) {
	 var date = new Date(); 
    date.setTime(date.getTime() - 10000); 
    document.cookie = key + "=a; expires=" + date.toGMTString(); 
}
/*
function showMsg(msg,error,title){
	//alert(msg);
	
	if("error"){
		if(title){
			swal(title, msg);
		}else{
			swal({title:"",text:msg});//timer
		}
	}else{
		if(title){
			swal(title, msg,"success");
		}else{
			swal("",msg,"success");
		}
	}
}
*/

function showMsg(message){
	var successOrError="#FFF";
	var level="alert-success";
	var time=3000;
	imgsrcTip='images/newBackstageIcon/notiice_warning@2x.png';
	if(arguments[1]!=null){
		if(arguments[1] == 'success'){
			successOrError= "#ED9C28";
			var imgsrcTip='images/newBackstageIcon/notice_ok@2x.png';
		}
	}
	if(arguments[3]!=null){
		time=arguments[3];
	}
	var width = $(window).width()/2;
	
	width = width-(message.length)*10/2;
	var tmp = "margin-left:"+width+"px;";
	/**
	 * textNotice 有问题
	 */
	//var msgDiv='<div class="textNotice" id="div_showmsg" style="max-width:230px;background-color:rgba(5,4,4,0.8);padding:20px 19px;text-align:justify;border-radius:3px;position:fixed;z-index:999999;top:50%;filter:alpha(Opacity=80);color:#fff;left:50%;transform:translate(-50%,-50%);">';
	var msgDiv='<div id="div_showmsg" style="max-width:230px;background-color:rgba(5,4,4,0.8);padding:20px 19px;text-align:justify;border-radius:3px;position:fixed;z-index:999999;top:50%;filter:alpha(Opacity=80);color:#fff;left:50%;transform:translate(-50%,-50%);">';
	msgDiv += '<p class="mulLine" style="padding-left:24px;position:relative;line-height:60px;line-height: 17px;">';
	msgDiv+='<img src='+imgsrcTip+' style="position:absolute;left:0;top:2px;" />';
	msgDiv+=''+message+'</p>';
	msgDiv+='</div>';

/*	var MsgDiv="<div  id='div_showmsg' class='  alert "+level+" fade in  offset4 ' style='position: fixed;;z-index:999999;top:200px;filter:alpha(Opacity=80);-moz-opacity:0.8;opacity: 0.8;background:#001;border-radius:5px;"+tmp+"padding:16px;'>"+
	""+
	"<p style='color:"+successOrError+";padding-top:0px;font-weight:bold;'>"+message+"</p>"+
	"</div>";
*/	if(document.getElementById('div_showmsg') != null){
		//已经存在了.
		$("#div_showmsg").remove();
	}
	$("body").append(msgDiv);
	//$(".alert").delay(time).hide("fast", function () { $(this).remove(); });
	$("#div_showmsg").delay(time).animate({'top':'hide'},function(){
		 //$(this).hide();
		 $(this).remove();
	});
}

/**
 * 底部自动.
 */
function footer_auto(loc){
	var location =  loc || 1;
	if(location == 1){
		$(".container_auto").css({"min-height":$(window).height()-130-122});
	}else if(location == 2){
		$(".container_auto").css({"min-height":$(window).height()-130-101});
	}
}


function loadMsg(){
	loadMsgForServer();
//	window.setInterval(function(){
//		loadMsgForServer();
//	},3000);
}

var isLogin =  0;
var isCompany = 0;
function loadMsgForServer(){
	//	if( !$(".guide").is(":visible") ){
	//	return;
	//}
	if( (window.location+"").indexOf("/login")  > 0){
		return;
	}
	if(isLogin != 1){
		return;
	}
	
	var dataJson = {apiUrl:"company/getNoReadCount",isCompany:isCompany};
	ajaxForUpdate(dataJson,function(rs){
		if(rs.num == 0){
			//$("#badgeCount").hide();
			$("#badgeCount p").removeClass("news");
		}else{
			$("#badgeCount").show();
			if(rs.num>99){				
				$("#badgeCount p").html(99);
			}else{
				$("#badgeCount p").html(rs.num);
			}
			$("#badgeCount p").addClass("news");
		}
		var announcement = rs.announcement;
		//console.log(announcement == undefined);
		if(announcement != null && announcement != undefined){
			$("#commonUpdatewindow").show();
			$("#commonUpdatewindow h1").html(announcement.title);
			$("#commonUpdatewindow p").html(announcement.content);
		}
	},{isShow:false,isAlert:false});
}


function hideToTop(){
	$(".guide").toggle();
}


function sign(str){
   var binarray = str+"";	
  var b64pad  = ""; 
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i += 3)
  {
    var triplet = (((binarray[i   >> 2] >> 8 * ( i   %4)) & 0xFF) << 16)
                | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
                |  ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
    for(var j = 0; j < 4; j++)
    {
      if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
      else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
    }
  }
  return str;
}



function c8Alert(msg,fn,calFn){
	if($("#c8Alert-2016").length > 0 ){
		//已经存在了.
		$("#c8Alert-2016 .wentitishi").html(msg);
		$("#c8Alert-2016").show();
		return;
	}
	var html=''; 
	html += '<div id="c8Alert-2016" class="window alert "><!--弹窗通用标题、关闭-->'; 
	html += '<div class="feedback tongyong">'; 
	html += '	<p class="window_popup  ml-10">提示</p>'; 
	html += '	<p style="position: absolute; right: 10px; top:5px" class="window_close"><a>X</a></p>'; 
	html += '	<div class="line"></div>'; 
	html += '	<p class="wentitishi">'+msg+'</p>'; 
	html += '	<div class="report_center mt-20 mb-20 f-r">'; 
	html += '	<button class="btn btn-warning radius f-r" >确定</button>'; 
	html += '	<button class="btn btn btn-default radius">取消</button>'; 
	html += '	</div>'; 
	html += '</div>'; 
	html += '</div>'; 
	$("body").append(html);
	$("#c8Alert-2016 .window_close").click(function(){
		$("#c8Alert-2016").hide();
	});
	$("#c8Alert-2016 .btn-warning").click(function(){
		if(fn){
			fn.call();
			$("#c8Alert-2016").hide();
		}else{
			$("#c8Alert-2016").hide();
		}
	});
	$("#c8Alert-2016 .btn-default").click(function(){
		if(calFn){
			calFn.call();
			$("#c8Alert-2016").hide();
		}else{
			$("#c8Alert-2016").hide();
		}
	});
}

function getCookie(name){ 
	var arr=document.cookie.split(';'); 
	for(var i=0;i<arr.length;i++){
		var arr2=arr[i].split('='); 
		if(arr2[0]==name){
			return arr2[1]; //找到所需要的信息返回出来 } 
			} 
		return ''; //找不到就返回空字符串 }
		}
}




/**
 * 验证经销商进入后台的密码
 */
function dealerPwd(type,comuid){

	//需要修改的地方: apiUrl:请求地址.
	ajaxForSelect({apiUrl:"dealer/getDealerById"},function(rs){
		var datas=rs.datas;
		//alert(JSON.stringify(datas))
		if(datas==undefined){//不是经销商
			//gotoPage("admin");
			changeClientType(comuid);
		}else{
			//console.log(comuid);
			if(datas.isOpenPwd==1){
				
				var pwdval=getCookie(deuids);
				if(pwdval==datas.pwd){
					//alert("存的："+pwdval+"------ 原的："+datas.pwd);
					//gotoPage("admin");
					changeClientType(comuid);
				}else{
					$('#canReaded').removeClass('hide');
					$('#canReaded').css('display',"block");
					$(".okLabel").attr("onclick","dealerPwd(2,"+comuid+")");
					if(type==2){
						var pwd = $("#dealerPassword").val();
						if(pwd == undefined || pwd == ""){
							showMsg("请输入密码！","error");
							return;
						}
						if(pwd!=datas.pwd){
							showMsg("密码错误！","error");
						}else{
							document.cookie=datas.dealerUid+'='+datas.pwd+';expires=0"'; 
							//alert(getCookie(deuids)+"--已存好");
							//gotoPage("admin");
							changeClientType(comuid);
							//gotoPage("wappages/dealerWelcome");
						}
					}
				}
				
				
//				//打开密码权限
//				var pwd = window.prompt("请输入进入后台的密码：","");
//				if(pwd == undefined || pwd == ""){
//					return;
//				}
//				if(pwd!=datas.pwd){
//					showMsg("密码错误！","erro");
//				}else{
//					gotoPage("admin");
//				}
			}else{
				//gotoPage("admin");
				changeClientType(comuid);
				//gotoPage("wappages/dealerWelcome");
			}
		}
	},{timeout:60000});//async:false,isShow:false,isAlert:false
	  document.onkeydown = function(e){
	        if(e.keyCode == 13){
	        	dealerPwd(2);
	        }
	    };
}